```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tomcat请求处理管道深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.8;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
            color: #222;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .highlight-box {
            border-left: 4px solid #4b6cb7;
            background-color: #f8fafc;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4.5rem;
            line-height: 0.65;
            margin: 0.1em 0.2em 0.1em 0;
            color: #4b6cb7;
            font-weight: 700;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-2/3 md:pr-10">
                    <h1 class="text-4xl md:text-5xl font-bold mb-6 leading-tight">Tomcat请求处理管道深度解析</h1>
                    <p class="text-xl mb-8 opacity-90">探索Tomcat从请求接收、解析、过滤到响应的完整生命周期，揭示高并发场景下的性能优化之道</p>
                    <div class="flex items-center">
                        <span class="mr-4"><i class="fas fa-clock mr-2"></i>阅读时间: 15分钟</span>
                        <span><i class="fas fa-calendar-alt mr-2"></i>2024年5月</span>
                    </div>
                </div>
                <div class="md:w-1/3 mt-10 md:mt-0">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1728737142529-dc47f0aa-f63e-4696-92b0-e450205ee1d4.png" alt="Tomcat架构图" class="rounded-lg shadow-xl transform rotate-2 border-4 border-white">
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <p class="drop-cap text-lg text-gray-700 mb-6">Tomcat是不可忽视的一部分，它在开发和生产环境中广泛应用于处理高并发请求。在Web应用中，用户请求往往是复杂的，Tomcat需要快速且高效地解析、处理并生成响应。Tomcat的Request处理管道，是贯穿整个请求处理流程的核心，它涉及从请求的接收、解析、过滤、路由到Servlet处理，再到最后生成并发送响应的每个步骤。</p>
                
                <div class="flex justify-center my-8">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1728737142529-dc47f0aa-f63e-4696-92b0-e450205ee1d4.png" alt="Tomcat请求处理流程" class="rounded-lg shadow-md w-full max-w-2xl">
                </div>
                
                <p class="text-lg text-gray-700 mb-6">通过了解Request处理管道，我们不仅能掌握Tomcat如何对请求进行有效的分发与处理，还能帮助我们更好地调优Tomcat性能，解决高并发下的瓶颈问题。特别是在面对复杂业务需求时，理解这一机制能够帮助我们扩展自定义逻辑，增强系统的可维护性和扩展性。</p>
            </div>
        </section>

        <!-- Architecture Overview -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-project-diagram"></i>
                </span>
                整体架构概述
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-blue-700">分层模块化设计</h3>
                    <p class="text-gray-700 mb-4">Tomcat 的整体架构设计遵循分层和模块化的原则，核心目标是高效处理 HTTP 请求并将其路由到合适的 Servlet。Tomcat 的架构从外向内主要由四大核心组件构成：</p>
                    <ul class="list-disc pl-6 space-y-2 text-gray-700">
                        <li><strong class="text-blue-600">Connector</strong>（连接器）</li>
                        <li><strong class="text-blue-600">Container</strong>（容器）</li>
                        <li><strong class="text-blue-600">Lifecycle</strong>（生命周期管理）</li>
                        <li><strong class="text-blue-600">Valves & Filters</strong>（阀门和过滤器）</li>
                    </ul>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-blue-700">协同工作机制</h3>
                    <p class="text-gray-700 mb-4">每个组件各司其职，同时相互协作，确保从接收到请求到生成响应的过程快速且稳定。</p>
                    <div class="highlight-box p-4 rounded">
                        <p class="text-gray-700"><i class="fas fa-info-circle text-blue-500 mr-2"></i>请求处理管道由 Connector 和 Container 协同完成，经过多层处理和过滤最终生成响应。</p>
                    </div>
                </div>
            </div>
            
            <div class="flex justify-center my-8">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1728737165083-9e909ed2-306c-4f97-8c0c-d4397f522b29.png" alt="Tomcat整体架构" class="rounded-lg shadow-md w-full max-w-2xl">
            </div>
        </section>

        <!-- Connector Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-plug"></i>
                </span>
                Connector的作用
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <p class="text-lg text-gray-700 mb-6">在 Tomcat 的架构中，<strong>Connector</strong> 是核心组件之一，其作用在于充当网络通信的桥梁，将外部的客户端请求转换为内部 Tomcat 能够理解的标准请求对象（即 <code>Request</code> 对象），并最终将生成的响应发送回客户端。</p>
                
                <div class="flex justify-center my-8">
                    <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1728737320325-cd53eb67-2d35-4dec-903b-07331af6b405.png" alt="Connector工作流程" class="rounded-lg shadow-md w-full max-w-2xl">
                </div>
                
                <div class="grid md:grid-cols-2 gap-6 mt-8">
                    <div class="bg-blue-50 p-5 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 text-blue-700 flex items-center">
                            <i class="fas fa-network-wired mr-2"></i>网络通信管理
                        </h3>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>通过套接字(Socket)处理网络请求</li>
                            <li>高效管理TCP连接(短连接/长连接)</li>
                            <li>通过线程池管理并发请求</li>
                            <li>支持异步I/O机制(NIO或APR)</li>
                        </ul>
                    </div>
                    
                    <div class="bg-blue-50 p-5 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 text-blue-700 flex items-center">
                            <i class="fas fa-exchange-alt mr-2"></i>协议解析与适配
                        </h3>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>支持HTTP/HTTPS协议解析</li>
                            <li>支持AJP二进制协议</li>
                            <li>将协议数据转换为内部Request对象</li>
                            <li>处理SSL/TLS加密解密</li>
                        </ul>
                    </div>
                    
                    <div class="bg-blue-50 p-5 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 text-blue-700 flex items-center">
                            <i class="fas fa-tasks mr-2"></i>线程池与并发控制
                        </h3>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>使用线程池避免频繁创建销毁线程</li>
                            <li>配置最大/最小线程数控制并发</li>
                            <li>异步I/O提升并发处理能力</li>
                            <li>连接复用减少TCP建立开销</li>
                        </ul>
                    </div>
                    
                    <div class="bg-blue-50 p-5 rounded-lg">
                        <h3 class="text-xl font-semibold mb-3 text-blue-700 flex items-center">
                            <i class="fas fa-tachometer-alt mr-2"></i>性能优化
                        </h3>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>HTTP长连接复用</li>
                            <li>请求限制与流量控制</li>
                            <li>超时处理和压力管理</li>
                            <li>高效资源管理(线程、内存)</li>
                        </ul>
                    </div>
                </div>
            </div>
        </section>

        <!-- Request & Response Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-code-branch"></i>
                </span>
                Request对象与Response对象的创建
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 text-blue-700">Request对象创建流程</h3>
                        <ol class="list-decimal pl-6 space-y-4 text-gray-700">
                            <li><strong>Connector接收并解析请求</strong>：将原始字节流转换为HTTP报文</li>
                            <li><strong>创建CoyoteRequest对象</strong>：封装网络协议相关细节</li>
                            <li><strong>适配到ServletRequest</strong>：转换为标准HttpServletRequest</li>
                            <li><strong>传递给Catalina容器</strong>：根据URI路由到对应Servlet</li>
                        </ol>
                        
                        <div class="mt-6 highlight-box p-4">
                            <p class="text-gray-700"><i class="fas fa-lightbulb text-yellow-500 mr-2"></i>Tomcat通过对象池复用Request对象，减少创建销毁开销，在高并发场景下显著提升性能。</p>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 text-blue-700">Response对象创建流程</h3>
                        <ol class="list-decimal pl-6 space-y-4 text-gray-700">
                            <li><strong>初始化CoyoteResponse</strong>：封装网络协议响应</li>
                            <li><strong>适配到ServletResponse</strong>：转换为标准HttpServletResponse</li>
                            <li><strong>业务逻辑处理</strong>：Servlet设置状态码、响应头等</li>
                            <li><strong>发送响应</strong>：转换为HTTP协议报文返回客户端</li>
                        </ol>
                        
                        <div class="mt-6">
                            <h4 class="font-semibold mb-2 text-gray-800">关键设计考量：</h4>
                            <ul class="list-disc pl-6 space-y-2 text-gray-700">
                                <li><strong>线程安全性</strong>：每个请求独占Request/Response对象</li>
                                <li><strong>扩展性</strong>：支持HTTP、HTTPS、AJP等多种协议</li>
                                <li><strong>生命周期管理</strong>：对象池复用与重置机制</li>
                            </ul>
                        </div>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="mt-10">
                    <h3 class="text-xl font-semibold mb-4 text-center text-blue-700">Request/Response生命周期示意图</h3>
                    <div class="mermaid">
                        graph LR
                            A[客户端请求] --> B[Connector接收]
                            B --> C[创建CoyoteRequest]
                            C --> D[适配为HttpServletRequest]
                            D --> E[容器处理]
                            E --> F[业务逻辑]
                            F --> G[创建HttpServletResponse]
                            G --> H[适配为CoyoteResponse]
                            H --> I[发送响应]
                            I --> J[客户端]
                    </div>
                </div>
            </div>
        </section>

        <!-- Container & Pipeline Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-sitemap"></i>
                </span>
                Container与Pipeline机制
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <h3 class="text-2xl font-semibold mb-6 text-blue-700">容器层次结构</h3>
                
                <div class="grid md:grid-cols-4 gap-4 mb-8">
                    <div class="bg-indigo-50 p-4 rounded-lg text-center">
                        <div class="bg-indigo-100 w-12 h-12 rounded-full flex items-center justify-center mx-auto mb-3">
                            <i class="fas fa-cogs text-indigo-600 text-xl"></i>
                        </div>
                        <h4 class="font-bold text-indigo-800">Engine</h4>
                        <p class="text-sm text-gray-600">顶层容器，管理多个虚拟主机</p>
                    </div>
                    
                    <div class="bg-purple-50 p-4 rounded-lg text-center">
                        <div class="bg-purple-100 w-12 h-12 rounded-full flex items-center justify-center mx-auto mb-3">
                            <i class="fas fa-server text-purple-600 text-xl"></i>
                        </div>
                        <h4 class="font-bold text-purple-800">Host</h4>
                        <p class="text-sm text-gray-600">虚拟主机，支持多应用部署</p>
                    </div>
                    
                    <div class="bg-blue-50 p-4 rounded-lg text-center">
                        <div class="bg-blue-100 w-12 h-12 rounded-full flex items-center justify-center mx-auto mb-3">
                            <i class="fas fa-box-open text-blue-600 text-xl"></i>
                        </div>
                        <h4 class="font-bold text-blue-800">Context</h4>
                        <p class="text-sm text-gray-600">Web应用上下文环境</p>
                    </div>
                    
                    <div class="bg-green-50 p-4 rounded-lg text-center">
                        <div class="bg-green-100 w-12 h-12 rounded-full flex items-center justify-center mx-auto mb-3">
                            <i class="fas fa-cube text-green-600 text-xl"></i>
                        </div>
                        <h4 class="font-bold text-green-800">Wrapper</h4>
                        <p class="text-sm text-gray-600">封装具体Servlet实例</p>
                    </div>
                </div>
                
                <h3 class="text-2xl font-semibold mb-6 text-blue-700">Pipeline与Valve机制</h3>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div>
                        <h4 class="font-semibold mb-3 text-gray-800 flex items-center">
                            <i class="fas fa-filter text-blue-500 mr-2"></i>Pipeline定义
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>每个Container都有自己的Pipeline</li>
                            <li>管理多个Valve的顺序执行</li>
                            <li>包含一个Basic Valve作为终点</li>
                            <li>提供链式请求处理机制</li>
                        </ul>
                        
                        <h4 class="font-semibold mt-6 mb-3 text-gray-800 flex items-center">
                            <i class="fas fa-project-diagram text-blue-500 mr-2"></i>设计优势
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>高扩展性：可插拔Valve组件</li>
                            <li>分离关注点：各组件职责单一</li>
                            <li>性能优化：共享处理上下文</li>
                            <li>灵活控制：可中断处理链</li>
                        </ul>
                    </div>
                    
                    <div>
                        <h4 class="font-semibold mb-3 text-gray-800 flex items-center">
                            <i class="fas fa-cog text-blue-500 mr-2"></i>Valve定义
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>Pipeline中的处理单元</li>
                            <li>可执行特定功能(如安全检查、日志)</li>
                            <li>支持自定义Valve开发</li>
                            <li>按配置顺序执行</li>
                        </ul>
                        
                        <div class="mt-6 bg-yellow-50 p-4 rounded-lg">
                            <h4 class="font-semibold mb-2 text-gray-800 flex items-center">
                                <i class="fas fa-code text-yellow-600 mr-2"></i>Valve示例代码
                            </h4>
                            <pre class="bg-gray-800 text-gray-100 p-3 rounded text-sm overflow-x-auto">
public class LoggingValve extends ValveBase {
    public void invoke(Request request, Response response) {
        // 记录请求日志
        logRequest(request);
        // 传递给下一个Valve
        getNext().invoke(request, response);
        // 记录响应日志
        logResponse(response);
    }
}</pre>
                        </div>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="mt-10">
                    <h3 class="text-xl font-semibold mb-4 text-center text-blue-700">Container与Pipeline协作流程图</h3>
                    <div class="mermaid">
                        graph TB
                            A[Connector] --> B[Engine Pipeline]
                            B --> B1[Valve1]
                            B1 --> B2[Valve2]
                            B2 --> B3[Basic Valve]
                            B3 --> C[Host Pipeline]
                            C --> C1[ValveA]
                            C1 --> C2[ValveB]
                            C2 --> C3[Basic Valve]
                            C3 --> D[Context Pipeline]
                            D --> D1[ValveX]
                            D1 --> D2[ValveY]
                            D2 --> D3[Basic Valve]
                            D3 --> E[Wrapper Pipeline]
                            E --> E1[ValveM]
                            E1 --> E2[ValveN]
                            E2 --> E3[Basic Valve]
                            E3 --> F[Servlet.service()]
                    </div>
                </div>
            </div>
        </section>

        <!-- Filter Chain Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-link"></i>
                </span>
                Filter Chain机制
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <div class="grid md:grid-cols-2 gap-8">
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 text-blue-700">Filter Chain核心概念</h3>
                        <p class="text-gray-700 mb-4">Filter Chain 是由多个过滤器(Filters)按特定顺序组成的链式结构，遵循责任链模式，提供灵活的请求预处理和响应后处理能力。</p>
                        
                        <div class="bg-blue-50 p-4 rounded-lg mb-6">
                            <h4 class="font-semibold mb-2 text-blue-700 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>关键特性
                            </h4>
                            <ul class="list-disc pl-6 space-y-2 text-gray-700">
                                <li>责任链模式实现模块化处理</li>
                                <li>可灵活控制请求传递</li>
                                <li>支持双向处理(请求/响应)</li>
                                <li>配置顺序决定执行顺序</li>
                            </ul>
                        </div>
                        
                        <h4 class="font-semibold mt-6 mb-3 text-gray-800">典型应用场景</h4>
                        <div class="grid grid-cols-2 gap-2">
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">安全认证</span>
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">日志记录</span>
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">请求修改</span>
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">响应压缩</span>
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">性能监控</span>
                            <span class="bg-green-100 text-green-800 text-sm px-3 py-1 rounded-full">跨域处理</span>
                        </div>
                    </div>
                    
                    <div>
                        <h3 class="text-2xl font-semibold mb-4 text-blue-700">Filter Chain工作流程</h3>
                        <ol class="list-decimal pl-6 space-y-3 text-gray-700">
                            <li><strong>请求进入Filter Chain</strong>：匹配配置的过滤器路径</li>
                            <li><strong>顺序执行过滤器</strong>：调用doFilter()方法传递请求</li>
                            <li><strong>请求预处理</strong>：身份验证、参数检查等</li>
                            <li><strong>到达Servlet</strong>：执行业务逻辑处理</li>
                            <li><strong>响应后处理</strong>：压缩、日志、修改响应等</li>
                            <li><strong>返回客户端</strong>：完成整个请求-响应周期</li>
                        </ol>
                        
                        <div class="mt-6 bg-gray-100 p-4 rounded-lg">
                            <h4 class="font-semibold mb-2 text-gray-800 flex items-center">
                                <i class="fas fa-code mr-2"></i>Filter示例代码
                            </h4>
                            <pre class="bg-gray-800 text-gray-100 p-3 rounded text-sm overflow-x-auto">
public class AuthFilter implements Filter {
    public void doFilter(ServletRequest req, ServletResponse res, 
            FilterChain chain) {
        // 1. 请求预处理 - 身份验证
        if (!authenticate(req)) {
            sendError(response);
            return; // 中断链
        }
        
        // 2. 传递到下一个Filter或Servlet
        chain.doFilter(req, res);
        
        // 3. 响应后处理
        logResponse(res);
    }
}</pre>
                        </div>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="mt-10">
                    <h3 class="text-xl font-semibold mb-4 text-center text-blue-700">Filter Chain处理流程图</h3>
                    <div class="mermaid">
                        graph LR
                            A[客户端请求] --> B[Filter1]
                            B -->|chain.doFilter()| C[Filter2]
                            C -->|chain.doFilter()| D[...]
                            D -->|chain.doFilter()| E[Servlet]
                            E --> F[生成响应]
                            F -->|返回| D
                            D -->|返回| C
                            C -->|返回| B
                            B -->|返回| A
                    </div>
                </div>
            </div>
        </section>

        <!-- Servlet Processing Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <span class="bg-blue-100 text-blue-800 p-2 rounded-full mr-4">
                    <i class="fas fa-cogs"></i>
                </span>
                Servlet处理
            </h2>
            
            <div class="bg-white rounded-xl shadow-md p-8 card-hover">
                <h3 class="text-2xl font-semibold mb-6 text-blue-700">Servlet生命周期</h3>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="bg-orange-50 p-5 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="bg-orange-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
                                <span class="text-orange-600 font-bold">1</span>
                            </div>
                            <h4 class="font-bold text-orange-800">初始化</h4>
                        </div>
                        <p class="text-sm text-gray-700">首次请求时调用init()方法，完成一次性初始化工作</p>
                    </div>
                    
                    <div class="bg-blue-50 p-5 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="bg-blue-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
                                <span class="text-blue-600 font-bold">2</span>
                            </div>
                            <h4 class="font-bold text-blue-800">请求处理</h4>
                        </div>
                        <p class="text-sm text-gray-700">每次请求调用service()或doGet()/doPost()方法</p>
                    </div>
                    
                    <div class="bg-purple-50 p-5 rounded-lg">
                        <div class="flex items-center mb-3">
                            <div class="bg-purple-100 w-10 h-10 rounded-full flex items-center justify-center mr-3">
                                <span class="text-purple-600 font-bold">3</span>
                            </div>
                            <h4 class="font-bold text-purple-800">销毁</h4>
                        </div>
                        <p class="text-sm text-gray-700">容器关闭时调用destroy()方法释放资源</p>
                    </div>
                </div>
                
                <h3 class="text-2xl font-semibold mb-4 text-blue-700">Servlet请求处理流程</h3>
                
                <div class="mb-8">
                    <ol class="list-decimal pl-6 space-y-3 text-gray-700">
                        <li><strong>接收请求</strong>：Tomcat将请求路由到匹配的Servlet</li>
                        <li><strong>调用service()方法</strong>：根据HTTP方法调用doGet()/doPost()等</li>
                        <li><strong>业务逻辑处理</strong>：访问数据库、调用服务等</li>
                        <li><strong>生成响应</strong>：设置状态码、响应头和内容</li>
                        <li><strong>返回响应</strong>：Tomcat将响应发送回客户端</li>
                    </ol>
                </div>
                
                <h3 class="text-2xl font-semibold mb-4 text-blue-700">关键对象</h3>
                
                <div class="grid md:grid-cols-2 gap-6 mb-8">
                    <div class="bg-gray-50 p-5 rounded-lg">
                        <h4 class="font-bold text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-arrow-right text-blue-500 mr-2"></i>HttpServletRequest
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>获取请求参数：getParameter()</li>
                            <li>读取请求体：getReader()</li>
                            <li>获取请求头：getHeader()</li>
                            <li>访问会话：getSession()</li>
                        </ul>
                    </div>
                    
                    <div class="bg-gray-50 p-5 rounded-lg">
                        <h4 class="font-bold text-gray-800 mb-3 flex items-center">
                            <i class="fas fa-arrow-left text-green-500 mr-2"></i>HttpServletResponse
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>设置状态码：setStatus()</li>
                            <li>设置响应头：setHeader()</li>
                            <li>输出响应：getWriter()</li>
                            <li>重定向：sendRedirect()</li>
                        </ul>
                    </div>
                </div>
                
                <h3 class="text-2xl font-semibold mb-4 text-blue-700">优化策略</h3>
                
                <div class="grid md:grid-cols-2 gap-4">
                    <div class="bg-green-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 text-green-800 flex items-center">
                            <i class="fas fa-bolt mr-2"></i>性能优化
                        </h4>
                        <ul class="list-disc pl-6 space-y-2 text-gray-700">
                            <li>减少阻塞操作(使用异步处理)</li>
                            <li>优化Session管理(使用Redis)</li>
                            <li>使用缓存机制</li>
                            <li>负载均衡与横向扩展</li>
                        </ul>
                    </div>
                    
                    <div class="bg-purple-50 p-4 rounded-lg">
                        <h4 class="font-semibold mb-2 text-purple-800 flex items-center">
                            <i class="fas fa-shield-alt mr-2"></i>线程安全
                        </h4>
                        <p class="text-gray-700">Servlet实例通常是单例多线程模式，避免使用实例变量或确保线程安全。</p>
                        <div class="mt-2 bg-purple-100 p-2 rounded text-sm">
                            <i class="fas fa-exclamation-triangle text-purple-600 mr-1"></i>
                            <span class="text-purple-800">建议使用局部变量而非实例变量</span>
                        </div>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="mt-10">
                    <h3 class="text-xl font-semibold mb-4 text-center text-blue-700">Servlet线程模型</h3>
                    <div class="mermaid">
                        graph TB
                            A[客户端1] -->|线程1| B[Servlet实例]
                            C[客户端2] -->|线程2| B
                            D[客户端3] -->|线程3| B
                            B --> E[共享资源?]
                            style E stroke:#f00,stroke-width:2px
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-blue-600 to-indigo-700 rounded-xl shadow-xl p-8 text-white">
                <h2 class="text-3xl font-bold mb-6">Tomcat请求处理管道总结</h2>
                
                <div class="grid md:grid-cols-3 gap-6">
                    <div>
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-layer-group mr-2"></i>分层处理
                        </h3>
                        <p class="opacity-90">Tomcat通过分层架构(Connector→Container)和Pipeline-Valve机制，实现了请求的高效分发与处理。</p>
                    </div>
                    
                    <div>
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-expand-arrows-alt mr-2"></i>扩展性
                        </h3>
                        <p class="opacity-90">通过自定义Valve和Filter，可以灵活扩展请求处理逻辑，满足各种业务需求。</p>
                    </div>
                    
                    <div>
                        <h3 class="text-xl font-semibold mb-3 flex items-center">
                            <i class="fas fa-tachometer-alt mr-2"></i>性能优化
                        </h3>
                        <p class="opacity-90">线程池、异步I/O、对象复用等机制确保Tomcat在高并发场景下的优秀表现。</p>
                    </div>
                </div>
                
                <div class="mt-8 bg-blue-800 bg-opacity-50 p-4 rounded-lg">
                    <h3 class="text-lg font-semibold mb-2 flex items-center">
                        <i class="fas fa-lightbulb mr-2"></i>关键设计模式
                    </h3>
                    <div class="grid grid-cols-3 gap-2">
                        <span class="bg-blue-700 bg-opacity-70 text-sm px-3 py-1 rounded-full">责任链模式</span>
                        <span class="bg-blue-700 bg-opacity-70 text-sm px-3 py-1 rounded-full">分层架构</span>
                        <span class="bg-blue-700 bg-opacity-70 text-sm px-3 py-1 rounded-full">模块化设计</span>
                        <span class="bg-blue-700 bg-opacity-70 text-sm px-3 py-1 rounded-full">对象池模式</span>
                        <span class="bg-blue-700 bg-opacity-70 text-sm px-3 py-1 rounded-full">生命周期管理</span>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-10">
        <div class="container mx-auto max-w-5xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-sm">探索技术原理，分享实践真知</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300 flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i>
                        http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-8 pt-8 text-center text-sm">
                <p>© 2024 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <!-- Mermaid JS -->
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```